home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / sg3-utils / examples / sg_simple16.c < prev    next >
Encoding:
C/C++ Source or Header  |  2007-03-12  |  3.4 KB  |  122 lines

  1. #include <unistd.h>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #include <sys/ioctl.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include "sg_lib.h"
  11. #include "sg_io_linux.h"
  12.  
  13. /* This program performs a READ_16 command as scsi mid-level support
  14.    16 byte commands from lk 2.4.15
  15.  
  16. *  Copyright (C) 2001 D. Gilbert
  17. *  This program is free software; you can redistribute it and/or modify
  18. *  it under the terms of the GNU General Public License as published by
  19. *  the Free Software Foundation; either version 2, or (at your option)
  20. *  any later version.
  21.  
  22.    Invocation: sg_simple16 <scsi_device>
  23.  
  24.    Version 1.02 (20020206)
  25.  
  26. */
  27.  
  28. #define READ16_REPLY_LEN 512
  29. #define READ16_CMD_LEN 16
  30.  
  31. #define EBUFF_SZ 256
  32.  
  33. int main(int argc, char * argv[])
  34. {
  35.     int sg_fd, k, ok;
  36.     unsigned char r16CmdBlk [READ16_CMD_LEN] =
  37.                 {0x88, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0};
  38.     sg_io_hdr_t io_hdr;
  39.     char * file_name = 0;
  40.     char ebuff[EBUFF_SZ];
  41.     unsigned char inBuff[READ16_REPLY_LEN];
  42.     unsigned char sense_buffer[32];
  43.  
  44.     for (k = 1; k < argc; ++k) {
  45.         if (*argv[k] == '-') {
  46.             printf("Unrecognized switch: %s\n", argv[k]);
  47.             file_name = 0;
  48.             break;
  49.         }
  50.         else if (0 == file_name)
  51.             file_name = argv[k];
  52.         else {
  53.             printf("too many arguments\n");
  54.             file_name = 0;
  55.             break;
  56.         }
  57.     }
  58.     if (0 == file_name) {
  59.         printf("Usage: 'sg_simple16 <sg_device>'\n");
  60.         return 1;
  61.     }
  62.  
  63.     if ((sg_fd = open(file_name, O_RDWR)) < 0) {
  64.         snprintf(ebuff, EBUFF_SZ,
  65.                  "sg_simple16: error opening file: %s", file_name);
  66.         perror(ebuff);
  67.         return 1;
  68.     }
  69.     /* Just to be safe, check we have a new sg device by trying an ioctl */
  70.     if ((ioctl(sg_fd, SG_GET_VERSION_NUM, &k) < 0) || (k < 30000)) {
  71.         printf("sg_simple16: %s doesn't seem to be an new sg device\n",
  72.                file_name);
  73.         close(sg_fd);
  74.         return 1;
  75.     }
  76.  
  77.     /* Prepare READ_16 command */
  78.     memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
  79.     io_hdr.interface_id = 'S';
  80.     io_hdr.cmd_len = sizeof(r16CmdBlk);
  81.     /* io_hdr.iovec_count = 0; */  /* memset takes care of this */
  82.     io_hdr.mx_sb_len = sizeof(sense_buffer);
  83.     io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
  84.     io_hdr.dxfer_len = READ16_REPLY_LEN;
  85.     io_hdr.dxferp = inBuff;
  86.     io_hdr.cmdp = r16CmdBlk;
  87.     io_hdr.sbp = sense_buffer;
  88.     io_hdr.timeout = 20000;     /* 20000 millisecs == 20 seconds */
  89.     /* io_hdr.flags = 0; */     /* take defaults: indirect IO, etc */
  90.     /* io_hdr.pack_id = 0; */
  91.     /* io_hdr.usr_ptr = NULL; */
  92.  
  93.     if (ioctl(sg_fd, SG_IO, &io_hdr) < 0) {
  94.         perror("sg_simple16: Inquiry SG_IO ioctl error");
  95.         close(sg_fd);
  96.         return 1;
  97.     }
  98.  
  99.     /* now for the error processing */
  100.     ok = 0;
  101.     switch (sg_err_category3(&io_hdr)) {
  102.     case SG_LIB_CAT_CLEAN:
  103.         ok = 1;
  104.         break;
  105.     case SG_LIB_CAT_RECOVERED:
  106.         printf("Recovered error on READ_16, continuing\n");
  107.         ok = 1;
  108.         break;
  109.     default: /* won't bother decoding other categories */
  110.         sg_chk_n_print3("READ_16 command error", &io_hdr, 1);
  111.         break;
  112.     }
  113.  
  114.     if (ok) { /* output result if it is available */
  115.         printf("READ_16 duration=%u millisecs, resid=%d, msg_status=%d\n",
  116.                io_hdr.duration, io_hdr.resid, (int)io_hdr.msg_status);
  117.     }
  118.  
  119.     close(sg_fd);
  120.     return 0;
  121. }
  122.